function [ newdata1,newdata2,newspace ] = LDA(data1,data2)
[h1,w1] = size(data1);
[h2,w2] = size(data2);
h = h1 + h2;

%input data must be in the same dimension
if w1 ~= w2
error('The input is something wrong!');
end

%initial output datas
newdata1 = zeros(h1,w1);
newdata2 = zeros(h2,w2);

%Expected matrix of input datas
E1 = mean(data1);
E2 = mean(data2);
EA = mean([data1;data2]);
fprintf('.');
%between-class scatter matrix
x1 = EA - E1;
x2 = EA - E2;
Sb = (h1*(x1'*x1) + h2*(x2'*x2))/h;
fprintf('.');
%within-class scatter matrix
y1 = 0;
for i = 1:h1
temp = data1(i,:)-E1;
y1 = y1 + temp'*temp;
end
y2 = 0;
for i = 1:h2
temp = data2(i,:)-E2;
y2 = y2 + temp'*temp;
end
Sw = h1*y1/h + h2*y2/h;
fprintf('.');

%find the best discriminant vectors
[V,L] = eig(Sw\Sb);
fprintf('.');

[~,b] = max(max(L));
newspace = V(:,b);
newdata1 = data1 * newspace;
newdata2 = data2 * newspace;
fprintf('\n');
end
